import 'package:eleapp/widget/emptydata.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../dio/http_request.dart';
import '../../dio/api.dart';

class Tasklist extends StatefulWidget {
  // 使用getx管理不用定义 arguments，直接Get.arguments获取传值
  // final Map arguments;
  // const Tasklist({super.key, required this.arguments});

  const Tasklist({super.key});

  @override
  State<Tasklist> createState() => _TasklistState();
}

class _TasklistState extends State<Tasklist> {
  List taskList = [];
  String classname = '班级实训';
  bool isReady = false;

  _load() async {
    List listRes = await Request2.post<List>(
        Api.practicelist, {'class_id': Get.arguments['class_id']});
    setState(() {
      isReady = true;
      taskList = listRes;
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    print(Get.arguments);
    Future.delayed(
      Duration.zero,
      () => setState(() {
        classname = Get.arguments['class_name'];
        _load();
      }),
    );
  }

  loadTask(List data) {
    List<Widget> treeList = [];
    for (int i = 0; i < data.length; i++) {
      treeList.add(Column(
        children: [
          ListTile(
            leading: const Icon(Icons.class_),
            title: Text('${data[i]['title']}'),
            trailing: const Icon(Icons.arrow_right_sharp),
            onTap: () {
              Get.toNamed('/grouplist', arguments: {
                'lesson_id': data[i]['id'],
                'lesson_name': data[i]['title']
              });
            },
          ),
          const Divider()
        ],
      ));
    }

    return treeList;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(classname),
        backgroundColor: Colors.blue,
      ),
      body: Container(
        padding: const EdgeInsets.only(top: 10),
        height: double.infinity,
        child: isReady
            ? taskList.isEmpty
                ? const EmptyData()
                : ListView(children: loadTask(taskList))
            : const SizedBox(),
      ),
    );
  }
}
